package com.hzy.controller;

import com.hzy.pojo.SysUser;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Package: com.hzy.controller
 * @ClassName: HelloController
 * @Author: 862982795
 * @CreateTime: 2021/07/13 20:23
 * @Description:第一个 Spring MVC 控制器
 */
@Controller
@RequestMapping(value = "/hello")
public class HelloController extends AbstractController {
    Logger logger = Logger.getLogger(HelloController.class);

    @RequestMapping(value = "/hello")
    public ModelAndView hello(@RequestParam(required = false) String realName) throws Exception {
//        logger.info("我的第一个 SpringMVC 程序，运行成功！");
        logger.info("你好 " + realName + " 欢迎来到 Spring MVC 课堂。");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("realName", realName);
        modelAndView.setViewName("hello");
        // 跳转到 hello 页面
        return modelAndView;
    }

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        System.out.println("Spring MVC 框架搭建成功。");
        return new ModelAndView("hello");
    }

    @RequestMapping(value = "/hello2")
    public String hello2(Model model, @RequestParam(required = false) String realName) throws Exception {
        logger.info("你好 " + realName + " 欢迎来到 Spring MVC 课堂。");
        model.addAttribute("realName", realName);
        model.addAttribute(realName);
        SysUser sysUser = new SysUser();
        sysUser.setRealName(realName);
        model.addAttribute("currentUser", sysUser);
        model.addAttribute(sysUser);
        // 跳转到 hello 页面
        return "hello";
    }

    @RequestMapping(value = "/hello3")
    public String hello3(Map<String, Object> map, @RequestParam(required = false) String realName) throws Exception {
//        logger.info("我的第一个 SpringMVC 程序，运行成功！");
        logger.info("你好 " + realName + " 欢迎来到 Spring MVC 课堂。");
        map.put("realName", realName);
        // 跳转到 hello 页面
        return "hello";
    }
}
